package com.saku.uiprojecttest.activities;

import android.content.Context;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.saku.uiprojecttest.R;
import com.saku.uiprojecttest.bean.PagerItem;

import java.util.List;

/**
 * User: Missa
 * Date: 2016-11-15
 * Time: 16:12
 * Description:
 */
public class CarViewAdapter extends RecyclerView.Adapter<CarViewAdapter.CarViewHoder>{

    private final List<PagerItem> list;
    private final Context mContext;

    public CarViewAdapter(Context context, List<PagerItem> list) {
        this.list = list;
        this.mContext = context;
    }

    @Override
    public CarViewHoder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, null);
        CarViewHoder cvh = new CarViewHoder(view);
        return cvh;
    }

    @Override
    public void onBindViewHolder(CarViewHoder holder, int position) {
        holder.imageView.setImageResource(list.get(position).resId);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class CarViewHoder extends RecyclerView.ViewHolder {
        protected ImageView imageView;

        public CarViewHoder(View itemView) {
            super(itemView);

            imageView = (ImageView) itemView.findViewById(R.id.imageview);
        }
    }

    protected static final class CarItemDecoration extends RecyclerView.ItemDecoration {
        private final int spacing;

        public CarItemDecoration(int spacing) {
            this.spacing = spacing;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            if (parent.getChildAdapterPosition(view) != 0) {
                outRect.left = spacing;
            } else {
                outRect.left = spacing*2;
            }
        }
    }
}
